﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using ElectronicCommerce.Framework.Web.ResourceManagement;

namespace ElectronicCommerce.Framework.Payment.Utility
{
    public static class HttpUtils
    {

        /// <summary>
        /// 发送Http同步请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="queryStrings"></param>
        /// <returns></returns>
        public static string SendSyncRequest(string url, NameValueCollection queryStrings)
        {
            return SendSyncRequest(url, queryStrings, "GET", Encoding.Default);
        }

        /// <summary>
        /// 发送Http同步请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="queryStrings"></param>
        /// <param name="contentEncoding"></param>
        /// <returns></returns>
        public static string SendSyncRequest(string url, NameValueCollection queryStrings, Encoding contentEncoding)
        {
            return SendSyncRequest(url, queryStrings, "GET", contentEncoding);
        }

        /// <summary>
        /// 发送Http同步请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="queryStrings"></param>
        /// <param name="httpMethod"></param>
        /// <returns></returns>
        public static string SendSyncRequest(string url, NameValueCollection queryStrings, string httpMethod)
        {
            return SendSyncRequest(url, queryStrings, httpMethod, Encoding.Default);
        }

        /// <summary>
        /// 发送Http同步请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="queryStrings"></param>
        /// <param name="httpMethod"></param>
        /// <param name="contentEncoding"></param>
        /// <returns></returns>
        public static string SendSyncRequest(string url, NameValueCollection queryStrings, string httpMethod, Encoding contentEncoding)
        {
            QueryStringBuilder queryStringBuilder = new QueryStringBuilder(queryStrings, contentEncoding);
            return SendSyncRequest(url, queryStringBuilder.ToString(), httpMethod, contentEncoding);
        }

        /// <summary>
        /// 发送Http同步请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="queryString"></param>
        /// <returns></returns>
        public static string SendSyncRequest(string url, string queryString)
        {
            return SendSyncRequest(url, queryString, "GET", Encoding.Default);
        }

        /// <summary>
        /// 发送Http同步请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="queryString"></param>
        /// <param name="contentEncoding"></param>
        /// <returns></returns>
        public static string SendSyncRequest(string url, string queryString, Encoding contentEncoding)
        {
            return SendSyncRequest(url, queryString, "GET", contentEncoding);
        }

        /// <summary>
        /// 发送Http同步请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="queryString"></param>
        /// <param name="httpMethod"></param>
        /// <returns></returns>
        public static string SendSyncRequest(string url, string queryString, string httpMethod)
        {
            return SendSyncRequest(url, queryString, httpMethod, Encoding.Default);
        }

        /// <summary>
        /// 发送Http同步请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="queryString">是用contentEncoding编码方式UrlEncode过的传递串。key=value&key2=value2</param>
        /// <param name="httpMethod">GET/POST</param>
        /// <param name="contentEncoding"></param>
        /// <returns></returns>
        public static string SendSyncRequest(string url, string queryString, string httpMethod, Encoding contentEncoding)
        {

            string responseText = "";
            if ((url == null) || (url == ""))
            {
                return null;
            }

            if (string.IsNullOrEmpty(queryString))
            {
                queryString = queryString.TrimStart('?');
            }

            if (string.IsNullOrEmpty(httpMethod))
            {
                httpMethod = "GET";
            }

            if (contentEncoding == null)
            {
                throw new ArgumentNullException("contentEncoding");
            }

            WebRequest webRequest = null;
            if (httpMethod.ToUpper() == "GET")
            {
                webRequest = WebRequest.Create(string.Format("{0}?{1}", url, queryString.TrimStart('?')));
                webRequest.Method = "GET";
            }
            else if (httpMethod.ToUpper() == "POST")
            {
                webRequest = WebRequest.Create(url);
                webRequest.Method = "POST";
                webRequest.ContentType = "application/x-www-form-urlencoded";

                byte[] bytes = contentEncoding.GetBytes(queryString);
                webRequest.ContentLength = bytes.Length;
                Stream requestStream = webRequest.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
            }

            try
            {
                responseText = new StreamReader(webRequest.GetResponse().GetResponseStream(), contentEncoding).ReadToEnd();
            }
            catch (Exception ex)
            {
                responseText = ex.Message;
            }

            return responseText;
        }
    }
}
